// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import iview from 'iview'
import 'iview/dist/styles/iview.css';
import router from './router/router'
import VueRouter from 'vue-router'
import contentmenu from 'v-contextmenu';
import 'v-contextmenu/dist/index.css';

import VueResource from 'vue-resource';
import vuex from 'vuex';
import axios from 'axios'
import store from './store'
import {url} from '../config/url'

/* eslint-disable no-new */
Vue.use(contentmenu);
Vue.config.productionTip = false;
Vue.prototype.$axios=axios;
Vue.use(iview);
Vue.use(VueRouter);
Vue.use(VueResource);

new Vue({
  el: '#app',
  render: h => h(App),
  router:router,
  store,
  template:'<App/>'
}).$mount('#app');

//异步请求前在header里加入token
axios.interceptors.request.use(
  config => {
    if(config.url===url+'/login'||config.url===url+'/register/register'||config.url===url+'/register/forget'){  //如果是登录和注册操作，则不需要携带header里面的token
    }else{
      if (localStorage.getItem('Authorization')) {
        config.headers.Authorization = localStorage.getItem('Authorization');
      }
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  });
//异步请求后，判断token是否过期
axios.interceptors.response.use(
  response =>{
    return response;
  },
  error => {
    if(error.response){
      switch (error.response.status) {
        case 401:
          localStorage.removeItem('Authorization');
          this.$router.push('/');
      }
    }
  }
);

//异步请求前判断请求的连接是否需要token
router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    next();
  } else {
    let token = localStorage.getItem('Authorization');
    if (token === 'null' || token === '') {
      next('/');
    } else {
      next();
    }
  }
});
